import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_sticky_header/flutter_sticky_header.dart';
import 'package:get/get.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:glass_kit/glass_kit.dart';
import 'package:todo_project/base_bg.dart';
import 'package:todo_project/color.dart';
import 'package:todo_project/data/task_data.dart';
import 'package:todo_project/home_page/home_control.dart';
import 'package:todo_project/home_page/home_drawer.dart';
import 'package:todo_project/item/task_item.dart';
import 'package:todo_project/router_manager.dart';

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  HomeControl homeControl = Get.put(HomeControl());

  @override
  void initState() {
    super.initState();
    homeControl.getImgUrl();
  }

  @override
  Widget build(BuildContext context) => BgContainer(
        Scaffold(
          appBar: AppBar(
            title: Text(
              "Today",
            ),
            foregroundColor: AppColor.textColor,
            backgroundColor: Colors.transparent,
          ),
          body: CustomScrollView(
            slivers: [
              SliverStickyHeader(
                overlapsContent: true,
                header: Padding(
                    child: const Text(
                      "进行中",
                      style: TextStyle(fontWeight: FontWeight.bold),
                    ),
                    padding:
                        EdgeInsets.symmetric(horizontal: 16.w, vertical: 20.w)),
                sliver: SliverPadding(
                  sliver: SliverAnimatedList(
                    itemBuilder: (ctx, index, anim) =>
                        TaskItem(taskData: TaskData()..title = "测试数据"),
                    initialItemCount: 20,
                  ),
                  padding: EdgeInsets.only(left: 60.w, top: 16.w),
                ),
              ),
              SliverStickyHeader(
                overlapsContent: true,
                header: Padding(
                    child: const Text(
                      "已完成",
                      style: TextStyle(fontWeight: FontWeight.bold),
                    ),
                    padding:
                        EdgeInsets.symmetric(horizontal: 16.w, vertical: 20.w)),
                sliver: SliverPadding(
                  sliver: SliverAnimatedList(
                    itemBuilder: (ctx, index, anim) =>
                        TaskItem(taskData: TaskData()..title = "测试数据"),
                    initialItemCount: 20,
                  ),
                  padding: EdgeInsets.only(left: 60.w, top: 16.w),
                ),
              )
            ],
          ),
          floatingActionButton: buildFloatingActionButton(),
          drawer: HomeDrawer(),
          drawerScrimColor: Colors.transparent,
        ),
      );

  /// 构建添加按钮
  ///
  FloatingActionButton buildFloatingActionButton() {
    return FloatingActionButton(
      onPressed: () {
        Get.toNamed(RouterManager.ROUTER_ADD_TASK);
      },
      backgroundColor: Colors.transparent,
      elevation: 0,
      child: GlassContainer(
        child: const Icon(
          Icons.add,
          color: AppColor.textColor,
        ),
        height: double.infinity,
        width: double.infinity,
        gradient: LinearGradient(
          colors: [
            Colors.white.withOpacity(0.60),
            Colors.white.withOpacity(0.20)
          ],
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
        ),
        borderGradient: LinearGradient(
          colors: [
            Colors.white.withOpacity(0.10),
            Colors.white.withOpacity(0.10),
            Colors.white.withOpacity(0.05),
            Colors.white.withOpacity(0.1)
          ],
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
          stops: [0.0, 0.39, 0.40, 1.0],
        ),
        blur: 20.0,
        borderWidth: 1.5,
        elevation: 3.0,
        isFrostedGlass: false,
        shadowColor: Colors.black.withOpacity(0.20),
        alignment: Alignment.center,
        frostedOpacity: 0.12,
        borderRadius: BorderRadius.circular(100),
        borderColor: Colors.transparent,
      ),
    );
  }

  /// 获取距离百分比
  ///
  double getPercent(BoxConstraints con, BuildContext context) {
    double percent =
        ((con.maxHeight - 150.w - (MediaQuery.of(context).padding.top)) /
            (250.w - 150.w));
    printInfo(info: "percent : $percent");
    if (percent < 0) {
      percent = 0.0;
    }
    return percent;
  }
}
